class Solution(object):
    def removeKdigits(self, num, k):
        """
        :type num: str
        :type k: int
        :rtype: str
        """
        n = len(num)
        if k == n:
            return '0'
        stack = [num[0]]
        for i in range(1, n):
            while stack and num[i] < stack[-1] and k > 0:
                stack.pop()
                k -= 1
            stack.append(num[i])
        out = ''.join(stack)
        if k > 0:
            out = out[:-k]
        return str(int(out))


data = Solution()
num = "1432219"
k = 3
print(data.removeKdigits(num, k))
